/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-12-04     huger       the first version
 */

#include "app_tft.h"
#include "bsp/bsp_converter.h"

#define DBG_TAG "tft"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

static uint8_t recv_buff[1024] = {0};
static uint16_t recv_len = 0;
static uint16_t recv_index = 0;


uint8_t tft_proc_buf[2][62]={0};


//uint8_t
void proc_frame(uint8_t* buf,uint8_t len)
{
    static uint16_t reg_address ,reg_offset;

    reg_address = bsp_cnv_arr_to_uint16(buf+4,true);
    if(reg_address>= 0x1100)
    {
        reg_offset = (reg_address-0x1100)*2;
        memcpy(tft_proc_buf[1]+reg_offset+4,buf+7,buf[6]*2);
        LOG_D("channel 2 recv a frame...");
        if(reg_address == 0x1100)
        {
            tft_proc_buf[1][3] = 1;
            bsp_uart2_tx_transmit(tft_proc_buf[1],61);
            LOG_D("channel 2 send a frame...");
        }
    }
    else{
        reg_offset = (reg_address-0x1000)*2;
        memcpy(tft_proc_buf[0]+reg_offset+4,buf+7,buf[6]*2);
        LOG_D("channel 1 recv a frame...");
        if(reg_address == 0x1000)
        {
            tft_proc_buf[0][3] = 0;
            bsp_uart2_tx_transmit(tft_proc_buf[0],61);
            LOG_D("channel 1 send a frame...");
        }
    }
}


void proc_one_byte(uint8_t data)
{
    if (recv_len == 0)
    {
        recv_buff[0] = recv_buff[1];
        recv_buff[1] = recv_buff[2];
        recv_buff[2] = data;
        if ((recv_buff[0] == 0x5a)&&(recv_buff[1]==0xa5))
        {
            recv_len = recv_buff[2]+3;
            recv_index = 3;
        }
    }
    else
    {
        recv_buff[recv_index++] = data;
//        recv_len = recv_index;

        if(recv_len >= 1024)
            recv_len = 0;

        if(recv_index >= recv_len)
        {
            proc_frame(recv_buff,recv_len);
            rt_kprintf("recv %d bytes:",recv_len);
            for(int8_t i=0;i<recv_len;i++)rt_kprintf("%x ",recv_buff[i]);
            rt_kprintf("\n");
            recv_len = 0;
        }
    }
}



/* 线程入口 */
static void thread_entry(void* parameter)
{
    rt_uint32_t value;

    while (1)
    {
        uint8_t data = 0xaa;
        if(bsp_uart3_rx_transmit(&data, RT_WAITING_FOREVER))
        {
            proc_one_byte(data);
        }
    }
}


void tft_init(void)
{
    #define THREAD_STACK_SIZE   2048
    #define THREAD_PRIORITY     15
    #define THREAD_TIMESLICE    20

    for(int i=0;i<2;i++)
    {
        tft_proc_buf[i][0]=0x5a;
        tft_proc_buf[i][1]=0xa5;
        tft_proc_buf[i][2]=0x39;
        tft_proc_buf[i][59]=0xa5;
        tft_proc_buf[i][60]=0x5a;
    }
    bsp_uart2_init();
    bsp_uart3_init();

    rt_thread_t tid = RT_NULL;
    /* 创建线程 1 */
    tid = rt_thread_create("tft",
                            thread_entry, RT_NULL,
                            THREAD_STACK_SIZE,
                            THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
}
